<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelHistory extends JModel
{
	/**
	 * History data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */
	function __construct() {
		parent::__construct();
	}
	
	/**
	 * Method to get history data
	 *
	 * @access public
	 * @return array
	*/
	function &getData() {

		global $mainframe, $option;
		
		//Initialise variables
		$user		= & JFactory::getUser();
		$ownuser = & Ownservice::getUser();
		
		//Set vars
		$id				= $user->get('id');
		$level			= $ownuser->get('level_id');
		$branchid	= $ownuser->get('branch_id');

		//$filter_status		= $mainframe->getUserStateFromRequest($option.'.history.filter_status', 'filter_status','','word');
		$filter_account		= $mainframe->getUserStateFromRequest($option.'.history.filter_account', 'filter_account','','word');

		$where = array();

		/*if($level == 3){
			$where[] = 't.transfer_id='.$id;	
		} else {
			$where[] = 't.user_id='.$id;
		}*/
	
		if($filter_account){
			$where[] = 't.account = '.$this->_db->Quote($filter_account);
		}
		
		/*$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		// Lets load the history transfer if it doesn't already exist
		if (empty($this->_data))
		{
			$query = 'SELECT t.*, a.account_name,b.name AS branchname, r.rate AS real_rate, c.iscore '
					.' FROM #__ownservice_transactions AS t'
					.' LEFT JOIN #__ownservice_accounts AS a ON a.account_no=t.account '
					.' LEFT JOIN #__ownservice_users AS u ON u.user_id=t.user_id '
					.' LEFT JOIN #__ownservice_branches AS b ON b.id=u.branch_id '
					.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id=t.rate_id '
					.$where
					.' GROUP BY t.id '
					.' ORDER BY t.created ASC';
			
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObjectList();
		}
		*/
	
		if($level == 3){
			$where[] = 't.transfer_id='.$id;
			$where[] = 't.state=4';
		} else {
			$where[] = 't.user_id='.$id;
			$where[] = 'curdate() < date_add(t.created,interval 10 day)';
		}
		$where[] 	= 'r.published = 1';
		if($level > 1 && $level < 5){	
			$where[] 	= 'u.branch_id='.$branchid;
		}
		
		$where 	= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
		
		$query = 'SELECT t.*'
				.' ,a.account_name '
				.' ,b.name AS branchname '
				.' ,r.rate AS real_rate, c.iscore, c.code '
				.' FROM #__ownservice_transactions AS t'
				.' LEFT JOIN #__ownservice_accounts AS a ON a.account_no=t.account '
				.' LEFT JOIN #__ownservice_users AS u ON u.user_id=t.user_id '
				.' LEFT JOIN #__ownservice_branches AS b ON b.id=u.branch_id '
				.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id=t.rate_id  '
				.' LEFT JOIN #__ownservice_currencies AS c ON c.id=r.currency_id '
				.$where
				.' GROUP BY t.id '
				.' ORDER BY t.created DESC';
			
		$this->_db->setQuery($query);
		$this->_data = $this->_db->loadObjectList();
		
		return $this->_data;
	}


	/**
	 * Method get user profile
	 *
	 * @return object
	 * @since 1.5
	*/
	function getProfile() {

		//Initialise variables
		$user	= & JFactory::getUser();
		$id		= $user->get('id');

		$query = ' SELECT p.*, u.name FROM #__ownservice_users AS p '
				.' LEFT JOIN #__users AS u ON u.id=p.user_id '
				.' WHERE u.id='.$id.' AND u.block = 0 ';

		$this->_db->setQuery($query);
		return $this->_db->loadObject();

	}

	/**
	 * Method get total transfer money
	 *
	 * @return object list
	 * @since 1.5
	*/
	function getTotalTransfer() {

		$user = & JFactory::getUser();

		$query = ' SELECT SUM(total) FROM #__ownservice_transactions '
				.' WHERE user_id = '.$user->get('id').' AND deleted=0 ';

		$this->_db->setQuery($query);
		$total = $this->_db->loadResult();

		return $total;
	}

	/**
	 * Method get accounts 
	 *
	 * @return object list
	 * @since 1.5
	*/
	function getAccounts() {

		$user = & JFactory::getUser();
		$id = $user->get('id');

		$query = ' SELECT * FROM #__ownservice_accounts '
				.' WHERE user_id = '.$id.' AND deleted = 0 ';

		$this->_db->setQuery($query);
		return $this->_db->loadObjectList();

	}
}
?>